package com.dhcc.bpm.modules.workflow.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.api.entity.Product;
import com.dhcc.bpm.modules.api.service.IProductApiService;
import com.dhcc.bpm.modules.api.service.IProductService;
import com.dhcc.bpm.modules.orga.service.*;
import com.dhcc.bpm.modules.workflow.dto.TaskCommitDto;
import com.dhcc.bpm.modules.workflow.dto.TaskDto;
import com.dhcc.bpm.modules.workflow.service.IWorkFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName WorkFlowController
 * @Description 访问控制API关联表 控制器
 * @Author xiafayu
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@RequestMapping("bpm/workflow")
@Api(value = "工作流任务提交", tags = "工作流任务提交接口")
public class WorkFlowController extends BladeController {

    @Autowired
    private IWorkFlowService iWorkFlowService;

    @Autowired
    private IProductApiService productApiService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IOrgaService orgaService;

    @Autowired
    private IAppService appService;

    @Autowired
    private ITestApplyService testApplyService;

    @Autowired
    private IProduceApplyService produceApplyService;

    @Autowired
    private IOnlineApplyService onlineApplyService;

    @Autowired
    private IProductService iProductService;

    /**
     * @Description 审批
     * @Author xiafayu
     * @Date 2020/2/24 20:37
     * @param list
     * @return com.dhcc.bpm.common.json.JSONObject
     * @throws
     **/
    @ApiLog("提交任务")
    @PostMapping("/commit")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "提交", notes = "提交任务")
    @Log(title = "提交任务",businessType = BusinessType.SELECT)
    public JSONObject commit(@Valid @RequestBody List<TaskCommitDto> list) {
        JSONObject json = new JSONObject();
        StringBuilder name = new StringBuilder();
       for(TaskCommitDto taskCommitDto : list){
           if("4".equals(taskCommitDto.getOpinionType())) {
               //发回重审
               String productName = taskCommitDto.getProductName();
               Product product = new Product();
               Integer status=0;
               product.setProductName(productName);
               Product product2=productService.selectProductByName(productName);
               if(product2!=null) {
                   if(product2.getStatus()!=null) {
                       status=product2.getStatus();
                       if(status==0) {
                           product.setStatus(9);
                       }else if(status==5) {
                           product.setStatus(1);
                       }
                   }

               }
               productService.updateStsByName(product);
           }
           json = iWorkFlowService.commit(taskCommitDto);
           if(json != null && json.getInt("code") == 200){
               if("chnl".equals(json.getStr("type"))){
                   iWorkFlowService.refreshApp(json.getStr("id"));
               }
               if("api".equals(json.getStr("type"))){
                   iWorkFlowService.refreshApi(json.getStr("id"));
               }
               continue;
           }
           else{
               if(null != iWorkFlowService.getNameById(json)){
                   name.append(iWorkFlowService.getNameById(json));
                   name.append(",");
               }
           }
       }
        if(StringUtils.isNotBlank(name)){
            json.put("msg", name + "审批失败");
        }
        return json;
    }

    /**
     * @Description 删除
     * @Author lizeyu
     * @Date 2020-6-10 14:38:01
     * @return com.dhcc.bpm.common.json.JSONObject
     * @throws
     **/
    @ApiLog("删除任务")
    @PostMapping("/delete")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除", notes = "删除任务")
    @Transactional
    @Log(title = "删除任务",businessType = BusinessType.DELETE)
    public R delete(@ApiParam(value = "executionId", required = true) @RequestParam String executionId,
                    @ApiParam(value = "appId", required = true) @RequestParam String appId,
                    @ApiParam(value = "taskId", required = true) @RequestParam String taskId) {
        return iWorkFlowService.delete(executionId, appId, taskId);
    }

    /**
     * @Description 待审批
     * @Author xiafayu
     * @Date 2020/2/24 20:39
     * @param taskDto
     * @return com.dhcc.bpm.common.json.JSONObject
     * @throws
     **/
    @ApiLog("待审批业务")
    @PostMapping("/taskList")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "待审批", notes = "待审批业务")
    @Transactional
    @Log(title = "待审批业务",businessType = BusinessType.SELECT)
    public JSONObject taskList(@Valid @RequestBody TaskDto taskDto) {
        return iWorkFlowService.taskList(taskDto, "t");
    }

    /**
     * @Description 已审批
     * @Author xiafayu
     * @Date 2020/2/24 20:39
     * @param taskDto
     * @return com.dhcc.bpm.common.json.JSONObject
     * @throws
     **/

    @PostMapping("/historyTaskList")
    @ApiLog("已审批业务")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "待审批", notes = "待审批业务")
    @Transactional
    @Log(title = "已审批业务",businessType = BusinessType.SELECT)
    public JSONObject historyTaskList(@Valid @RequestBody TaskDto taskDto) {
        return iWorkFlowService.taskList(taskDto, "historyT");
    }

    /**
     * @Description 删除
     * @Author wjh
     * @Date 2021年2月20日16:56:31
     * @throws
     **/
    @ApiLog("删除任务")
    @PostMapping("/deleteRelated")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除", notes = "删除任务")
    @Transactional
    @Log(title = "删除任务",businessType = BusinessType.DELETE)
    public R deleteRelated(@ApiParam(value = "type", required = true) @RequestParam String type,
                    @ApiParam(value = "appId", required = true) @RequestParam String appId) {
        return iWorkFlowService.deleteRelated(type, appId);

    }
}
